/*
 * CreateAccount.java
 *
 * Created on 5 novembre 2007, 14:30
 */
package com.reimscopie.console ;

import java.io.* ;

import javax.naming.* ;
import javax.transaction.* ;

import com.reimscopie.* ;
import com.reimscopie.beans.interfaces.* ;


/**
 * This command creates a new user account.
 */
public class CreateAccount extends BeanCommand {
  
  /**
   * Create a new <code>CreateAccount</code> instance.
   */
  public CreateAccount() {
    
    super("com.reimscopie.beans.session.ReimsCopieAdminBean",
        ReimsCopieAdminRemote.class) ;
    
  }
  
  
  /**
   * Return the name of this command. The name is the first word in the shell
   * command line.
   *
   * @return The command name.
   */
  public String getName() {
    return "create-account" ;
  }
  
  
  /**
   * Return a short help message about this command.
   *
   * @return The command help.
   */
  public String getHelp() {
    return "Create a new user account" ;
  }
  
  
  /**
   * Return the command usage. This string must give all command options with
   * a description for each option.
   *
   * @return The command usage.
   */
  public String getUsage() {
    StringBuilder builder = new StringBuilder() ;
        
    builder.append("create-account -login l -mail m -password p -newsletter l " +
        "-balance b -name n -firstname fn -street s -city c -postal-code c " +
        "-phone p -country c\n") ;
    builder.append("  login          The user login\n") ;
    builder.append("  password       The user password\n") ;
    builder.append("  mail           The user e-mail\n") ;
    builder.append("  newsletter     If the user wants to receive the newsletter or not (boolean)\n") ;
    builder.append("  balance        The money amount of this user") ;
    builder.append("  name           The user name\n") ;
    builder.append("  firstname      The user firstname\n") ;
    builder.append("  street         The street in the address\n") ;
    builder.append("  city           The city in the address\n") ;
    builder.append("  postal-code    The postal code in the address\n") ;
    builder.append("  phone          The user phone number\n") ;
    builder.append("  country        The country in the address\n") ;
    
    return builder.toString() ;
  }
  
  
  /**
   * Execute this command.
   *
   * @param args The arguments given in the command line.
   * @param output The command output.
   *
   * @exception CommandException Arguments are invalid.
   */
  public void execute(String[] args,ConsoleOutput output)
  throws CommandException {
    
    String login       = requireOption("login",args) ;
    String password    = requireOption("password",args) ;
    String mail        = requireOption("mail",args) ;
    boolean newsletter = requireBooleanOption("newsletter",args) ;
    float balance      = requireFloatOption("balance",args) ;
    
    String name        = requireOption("name",args) ;
    String firstname   = requireOption("firstname",args) ;
    String street      = requireOption("street",args) ;
    String city        = requireOption("city",args) ;
    String postalCode  = requireOption("postal-code",args) ;
    String phone       = requireOption("phone",args) ;
    String country     = requireOption("country",args) ;
    
    PersonalInfo info = new PersonalInfo(
        name,
        firstname,
        street,
        city,
        postalCode,
        phone,
        country) ;
    
    Account account = new Account(
        login,
        password,
        mail,
        newsletter,
        balance,
        info) ;
    
    doCreate(account,output) ;
    
  }
  
  
  /**
   * Create the account.
   *
   * @param account The account to create.
   * @param output The command output.
   */
  private void doCreate(Account account,ConsoleOutput output) {
    
    try {
      // get JNDI initial context
      Context context = new InitialContext() ;
      UserTransaction transaction = getTransaction(context) ;
      ReimsCopieAdminRemote bean = (ReimsCopieAdminRemote)getBean(context) ;
      
      if (bean == null) {
        output.printError("account not created (no bean)",true) ;
        return ;
      }
      
      try {
        transaction.begin() ;
        
        // existing account
        if (bean.getAccount(account.getLogin()) != null) {
          output.printError("login already used",true) ;
          transaction.rollback() ;
          return ;
        }
        
        bean.createAccount(account) ;
        transaction.commit() ;
        output.print("Done",true) ;
      }
      catch (Exception e) {
        output.printError("Creation failed",true) ;
        try {
          transaction.rollback() ;
        }
        catch (SystemException ex) {
          output.printError(ex) ;
        }
        output.printError(e) ;
      }
      
    }
    catch (NamingException e) {
      output.printError("Cannot get initial context for JNDI") ;
      output.printError(e) ;
      return ;
    }
  }
  
}
